<?php
/**
 * //w/w/w.s/h/u/o/t//u/p/u c/o/m  说/图/谱/源/码/分/享
 */
defined("IN_IA")or exit("Access Denied");
checklogin();  //验证是否登录
global $_W,$_GPC;
$uniacid=$_W['uniacid'];
$op=$_GPC['op'] ? $_GPC['op'] : "shop_order";

//商品订单列表
if($op=='shop_order'){
    $flag=$_GPC['flag'];
    $order_type=$_GPC['order_type'];
    $condition = array();
    $old_time = array(
        'start' => date("Y-m-d", strtotime('-30 days')),
        'end' => date('Y-m-d', strtotime('+1 days'))
    );
    if (!empty($_GPC['order_number'])) {
        $order_number = trim($_GPC['order_number']);
        $condition['order_number LIKE'] = '%' . $order_number . '%';
    }
    //订单状态  1/全部订单 2/未支付 3/待发货 4/待收货 5/已完成 6/已取消
    if($_GPC['is_recycel']){
        $is_recycel=$_GPC['is_recycel'];
        $condition['is_recycel']=$is_recycel;
    }else{
        if (!empty($_GPC['status']) && $_GPC['status'] != 6) {
            $status = $_GPC['status'];
            if ($status == 7) {
                $condition['status'] = 0;
            } else {
                $condition['status'] = $status;
            }
        } else {
            $status =6;
        }
        $condition['is_recycel']=0;
    }
    $time = $_GPC['time'];
    if ($time) {
        $condition['create_time >'] = strtotime($time['start']);
        $condition['create_time <'] = strtotime($time['end']);
        $old_time = $time;
    }
    $condition['uniacid'] = $uniacid;
    $condition['order_type'] = 0;
    //导出订单
    if($flag=='true'){
        outOrder($condition,$order_type);
    }else {//查看订单信息
        if ($_GPC['order_number'] || $_GPC['time']) {
            $list = pdo_getall("cqkundian_farm_shop_order", $condition, '', '', 'create_time desc');
            $totalMoney = 0;
            for ($i = 0; $i < count($list); $i++) {
                $totalMoney += $list[$i]['total_price'];
            }
        } else {
            $listCount = pdo_getall("cqkundian_farm_shop_order", $condition);
            $setData = pdo_get('cqkundian_farm_set', array('ikey' => 'expire_order_time', 'uniacid' => $uniacid));
            if (!empty($setData) && $setData['value'] != 0 && $setData['value'] > 0) {
                for ($i = 0; $i < count($listCount); $i++) {
                    if ($listCount[$i]['status'] == 0) {
                        if ($listCount[$i]['create_time'] + $setData['value'] <= time()) {
                            pdo_update('cqkundian_farm_shop_order', array('status' => 5), array('uniacid' => $uniacid, 'id' => $listCount[$i]['id']));
                        }
                    }
                }
            }
            $totalMoney = 0;
            for ($i = 0; $i < count($listCount); $i++) {
                $totalMoney += $listCount[$i]['total_price'];
            }
            $total = count($listCount);
            $pageSize = 12; //每页显示的数据条数
            $pageIndex = intval($_GPC['page']) ? intval($_GPC['page']) : 1;  //当前页
            $pager = pagination($total, $pageIndex, $pageSize);
            $list = pdo_getall("cqkundian_farm_shop_order", $condition, '', '', 'create_time desc', array($pageIndex, $pageSize));
        }
    }
    include $this->template('web/order/shop_order');
}

//查看订单详情
if($op=='shop_order_edit'){
    $order_id=$_GPC['id'];
    $orderData=pdo_get('cqkundian_farm_shop_order',array('id'=>$order_id,'uniacid'=>$uniacid));
    $order_detail=pdo_getall('cqkundian_farm_shop_order_detail',array('order_id'=>$order_id,'uniacid'=>$uniacid));
    //查询商品规格
    for($i=0;$i<count($order_detail);$i++){
        $specVal=pdo_get('cqkundian_farm_goods_spec',array('goods_id'=>$order_detail[$i]['goods_id'],'uniacid'=>$uniacid));
        $spec_id=explode(",", $specVal['sku_name']);
        $spec_val=pdo_getall('cqkundian_farm_spec_value',array('id in'=>$spec_id));
        $specItem=pdo_getall('cqkundian_farm_spec',array('goods_id'=>$order_detail[$i]['goods_id'],'uniacid'=>$uniacid));
        for($j=0;$j<count($specItem);$j++){
            for($m=0;$m<count($spec_val);$m++){
                if($specItem[$j]['id']==$spec_val[$m]['spec_id']){
                    $specItem[$j]['spec_value']=$spec_val[$m]['spec_value'];
                }
            }
        }
        $order_detail[$i]['specItem']=$specItem;
    }
    include $this->template("web/order/order_detail");
}

//开始配送
if($op=='send_goods'){
    $order_id=$_GPC['order_id'];
    $send_number=$_GPC['send_number'];
    $orderData=pdo_get('cqkundian_farm_shop_order',array('id'=>$order_id,'uniacid'=>$uniacid));
    $save_date=array(
        'send_number'=>$send_number,
        'express_company'=>$_GPC['express_company'],
    );
    if($orderData['order_type']==0 || $orderData['order_type']==2|| $orderData['order_type']==3||$orderData['order_type']==4){
        $save_date['status']=2;
    }elseif($orderData['order_type']==1){
        $save_date['group_status']=2;
    }
    $res=pdo_update('cqkundian_farm_shop_order',$save_date,array('id'=>$order_id,'uniacid'=>$uniacid));
    echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
}



//完成配送
if($op=='confirmGoods'){
    $order_id=$_GPC['order_id'];
    $orderData=pdo_get('cqkundian_farm_shop_order',array('id'=>$order_id,'uniacid'=>$uniacid));
    //判断当前订单是否需要发放佣金
    $update_order=array(
        'status'=>3,
    );
    if($orderData['is_price']==1){
        $result=saleSendPirce($orderData,$uniacid,1);
        if($result){
            //修改佣金发放状态
            $update_order['is_price']=2;
        }
    }
    $res=pdo_update('cqkundian_farm_shop_order',$update_order,array('uniacid'=>$uniacid,'id'=>$order_id));
    echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
}

//订单移入回收站
if($op=='move_into_recycel'){
    $order_id=$_GPC['order_id'];
    $type=$_GPC['type'];
    if($type==1) {
        $update_order=array('is_recycel'=>1);
    }else{
        $update_order=array('is_recycel'=>0);
    }
    $res = pdo_update('cqkundian_farm_shop_order', $update_order, array('uniacid' => $uniacid, 'id' => $order_id));
    if($res){
        echo json_encode(array('status'=>1,'msg'=>'操作成功'));die;
    }else{
        echo json_encode(array('status'=>2,'msg'=>'操作失败'));die;
    }
}

//删除订单
if($op=='deleteOrder'){
    $order_id=$_GPC['order_id'];
    $orderData=pdo_get('cqkundian_farm_shop_order',array('uniacid'=>$uniacid,'id'=>$order_id));
    if($orderData['status']==0 || $orderData['status']==5){
        $res=pdo_delete('cqkundian_farm_shop_order',array('id'=>$order_id,'uniacid'=>$uniacid));
        $res_detail=pdo_delete('cqkundian_farm_shop_order_detail',array('order_id'=>$order_id,'uniacid'=>$uniacid));
        echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
    }else{
        echo json_encode(array('status'=>3));die;
    }
}

//组团订单列表
if($op=='group_order'){
    $flag=$_GPC['flag'];
    $order_type=$_GPC['order_type'];
    $old_time=array(
        'start'=>date('Y-m-d',strtotime('-30 days')),
        'end'=>date('Y-m-d',strtotime('+1 days')),
    );
    $condition=array();
    if(!empty($_GPC['order_number'])){
        $order_number=trim($_GPC['order_number']);
        $condition['order_number LIKE']= '%'.$order_number.'%';
    }
     if($_GPC['is_recycel']){
        $is_recycel=$_GPC['is_recycel'];
        $condition['is_recycle']=$is_recycel;
    }else{
        if (!empty($_GPC['status']) && $_GPC['status'] != 8) {
            $status = $_GPC['status'];
            if ($status == 7) {
                $condition['status'] = 0;
            } else {
                $condition['status'] = $status;
            }
        } else {
            $status = 8;
        }    
        $condition['is_recycle']=0;
    }
    

    $time=$_GPC['time'];
    if($time){
        $condition['create_time >']=strtotime($time['start']);
        $condition['create_time <']=strtotime($time['end']);
        $old_time=$time;
    }
    $condition['uniacid']=$uniacid;

    if($flag=='true'){
        outOrder($condition,$order_type);
    }else {

        if ($_GPC['order_number']  || $_GPC['time']) {
            $list = pdo_getall("cqkundian_farm_group_order", $condition, '', '', 'create_time desc');
            $totalMoney = 0;
            for ($i = 0; $i < count($list); $i++) {
                $totalMoney += $list[$i]['total_price'];
            }
        } else {
            $listCount = pdo_getall("cqkundian_farm_group_order", $condition);
            $setData = pdo_get('cqkundian_farm_set', array('ikey' => 'expire_order_time', 'uniacid' => $uniacid));
            if (!empty($setData) && $setData['value'] != 0 && $setData['value'] > 0) {
                for ($i = 0; $i < count($listCount); $i++) {
                    if ($listCount[$i]['status'] == 0) {
                        if ($listCount[$i]['create_time'] + $setData['value'] <= time()) {
                            pdo_update('cqkundian_farm_group_order', array('status' => 5), array('uniacid' => $uniacid, 'id' => $listCount[$i]['id']));
                        }
                    }
                }
            }

            $totalMoney = 0;
            for ($i = 0; $i < count($listCount); $i++) {
                $totalMoney += $listCount[$i]['total_price'];
            }
            $total = count($listCount);
            $pageSize = 15; //每页显示的数据条数
            $pageIndex = intval($_GPC['page']) ? intval($_GPC['page']) : 1;  //当前页
            $pager = pagination($total, $pageIndex, $pageSize);
            $list = pdo_getall("cqkundian_farm_group_order", $condition, '', '', 'create_time desc', array($pageIndex, $pageSize));
        }
    }
    include $this->template('web/order/group_order');
}

//编辑组团订单信息
if($op=='group_edit'){
    $order_id=$_GPC['id'];
    $orderData=pdo_get('cqkundian_farm_group_order',array('id'=>$order_id,'uniacid'=>$uniacid));
    $order_detail=pdo_getall('cqkundian_farm_group_order_detail',array('order_id'=>$order_id,'uniacid'=>$uniacid));
    //查询商品规格
    for($i=0;$i<count($order_detail);$i++){
        $specVal=pdo_get('cqkundian_farm_group_goods_spec',array('goods_id'=>$order_detail[$i]['goods_id'],'uniacid'=>$uniacid));
        $spec_id=explode(",", $specVal['sku_name']);
        $spec_val=pdo_getall('cqkundian_farm_group_spec_value',array('id in'=>$spec_id));
        $specItem=pdo_getall('cqkundian_farm_group_spec',array('goods_id'=>$order_detail[$i]['goods_id'],'uniacid'=>$uniacid));
        for($j=0;$j<count($specItem);$j++){
            for($m=0;$m<count($spec_val);$m++){
                if($specItem[$j]['id']==$spec_val[$m]['spec_id']){
                    $specItem[$j]['spec_value']=$spec_val[$m]['spec_value'];
                }
            }
        }
        $order_detail[$i]['specItem']=$specItem;
    }
    include $this->template("web/order/order_detail");
}

//组团订单回收站
if($op=='move_into_group_recycel'){
    $order_id=$_GPC['order_id'];
    $type=$_GPC['type'];
    if($type==1) {
        $update_order=array('is_recycle'=>1);
    }else{
        $update_order=array('is_recycle'=>0);
    }
    $res = pdo_update('cqkundian_farm_group_order', $update_order, array('uniacid' => $uniacid, 'id' => $order_id));
    if($res){
        echo json_encode(array('status'=>1,'msg'=>'操作成功'));die;
    }else{
        echo json_encode(array('status'=>2,'msg'=>'操作失败'));die;
    }
}

//组团订单开始配送
if($op=='send_group_goods'){
    $order_id=$_GPC['order_id'];
    $send_number=$_GPC['send_number'];
    $orderData=pdo_get('cqkundian_farm_group_order',array('id'=>$order_id,'uniacid'=>$uniacid));
    $save_date=array(
        'send_number'=>$send_number,
        'express_company'=>$_GPC['express_company'],
        'status'=>2,
    );
    $res=pdo_update('cqkundian_farm_group_order',$save_date,array('id'=>$order_id,'uniacid'=>$uniacid));
    echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
}

//完成配送
if($op=='confirmGroupGoods'){
    $order_id=$_GPC['order_id'];
    $orderData=pdo_get('cqkundian_farm_group_order',array('id'=>$order_id,'uniacid'=>$uniacid));
    //判断当前订单是否需要发放佣金
    $update_order=array(
        'status'=>3,
    );
    if($orderData['is_price']==1){
        $result=saleSendPirce($orderData,$uniacid,2);
        if($result){
            //修改佣金发放状态
            $update_order['is_price']=2;
        }
    }
    $res=pdo_update('cqkundian_farm_group_order',$update_order,array('uniacid'=>$uniacid,'id'=>$order_id));
    echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
}


//删除订单
if($op=='deleteGroupOrder'){
    $order_id=$_GPC['order_id'];
    $orderData=pdo_get('cqkundian_farm_shop_order',array('uniacid'=>$uniacid,'id'=>$order_id));
    if($orderData['status']==0 || $orderData['status']==5){
        $res=pdo_delete('cqkundian_farm_group_order',array('id'=>$order_id,'uniacid'=>$uniacid));
        $res_detail=pdo_delete('cqkundian_farm_group_order_detail',array('order_id'=>$order_id,'uniacid'=>$uniacid));
        echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
    }else{
        echo json_encode(array('status'=>3));die;
    }
}


//积分商城订单
if($op=='integral_order'){
    $flag=$_GPC['flag'];
    $order_type=$_GPC['order_type'];
    $old_time=array(
        'start'=>date('Y-m-d',strtotime('-30 days')),
        'end'=>date('Y-m-d',strtotime('+1 days')),
    );
    $condition=array();
    if(!empty($_GPC['order_number'])){
        $order_number=trim($_GPC['order_number']);
        $condition['order_number LIKE']= '%'.$order_number.'%';
    }
    if($_GPC['is_recycel']){
        $is_recycel=$_GPC['is_recycel'];
        $condition['is_recycle']=$is_recycel;
    }else{
        if (!empty($_GPC['status']) && $_GPC['status'] != 6) {
            $status = $_GPC['status'];
            if ($status == 7) {
                $condition['status'] = 0;
            } else {
                $condition['status'] = $status;
            }
        } else {
            $status = 6;
        }
        $condition['is_recycle']=0;
    }

    $time=$_GPC['time'];
    if($time){
        $condition['create_time >']=strtotime($time['start']);
        $condition['create_time <']=strtotime($time['end']);
        $old_time=$time;
    }
    $condition['uniacid']=$uniacid;
    if($flag=='true'){
        outOrder($condition,$order_type);
    }else {
        if ($_GPC['order_number'] || $_GPC['status'] || $_GPC['time']) {
            $list = pdo_getall("cqkundian_farm_integral_order", $condition, '', '', 'create_time desc');
            $totalMoney = 0;
            for ($i = 0; $i < count($list); $i++) {
                $totalMoney += $list[$i]['total_price'];
            }
        } else {
            $listCount = pdo_getall("cqkundian_farm_integral_order", $condition);
            $setData = pdo_get('cqkundian_farm_set', array('ikey' => 'expire_order_time', 'uniacid' => $uniacid));
            if (!empty($setData) && $setData['value'] != 0 && $setData['value'] > 0) {
                for ($i = 0; $i < count($listCount); $i++) {
                    if ($listCount[$i]['status'] == 0) {
                        if ($listCount[$i]['create_time'] + $setData['value'] <= time()) {
                            pdo_update('cqkundian_farm_integral_order', array('status' => 5), array('uniacid' => $uniacid, 'id' => $listCount[$i]['id']));
                        }
                    }
                }
            }
            $totalMoney = 0;
            for ($i = 0; $i < count($listCount); $i++) {
                $totalMoney += $listCount[$i]['total_price'];
            }
            $total = count($listCount);
            $pageSize = 15; //每页显示的数据条数
            $pageIndex = intval($_GPC['page']) ? intval($_GPC['page']) : 1;  //当前页
            $pager = pagination($total, $pageIndex, $pageSize);
            $list = pdo_getall("cqkundian_farm_integral_order", $condition, '', '', 'create_time desc', array($pageIndex, $pageSize));
        }
    }
    include $this->template('web/order/integral_order');
}   

if($op=='integral_edit'){
    $order_id=$_GPC['id'];
    $orderData=pdo_get('cqkundian_farm_integral_order',array('id'=>$order_id,'uniacid'=>$uniacid));
    $order_detail=pdo_getall('cqkundian_farm_integral_order_detail',array('order_id'=>$order_id,'uniacid'=>$uniacid));
    //查询商品规格
    for($i=0;$i<count($order_detail);$i++){
        $specVal=pdo_get('cqkundian_farm_integral_goods_spec',array('goods_id'=>$order_detail[$i]['goods_id'],'uniacid'=>$uniacid));
        $spec_id=explode(",", $specVal['sku_name']);
        $spec_val=pdo_getall('cqkundian_farm_integral_spec_value',array('id in'=>$spec_id));
        $specItem=pdo_getall('cqkundian_farm_integral_spec',array('goods_id'=>$order_detail[$i]['goods_id'],'uniacid'=>$uniacid));
        for($j=0;$j<count($specItem);$j++){
            for($m=0;$m<count($spec_val);$m++){
                if($specItem[$j]['id']==$spec_val[$m]['spec_id']){
                    $specItem[$j]['spec_value']=$spec_val[$m]['spec_value'];
                }
            }
        }
        $order_detail[$i]['specItem']=$specItem;
    }
    include $this->template("web/order/order_detail");
}

//移入回收站
if($op=='move_into_integral_recycel'){
        $order_id=$_GPC['order_id'];
    $type=$_GPC['type'];
    if($type==1) {
        $update_order=array('is_recycle'=>1);
    }else{
        $update_order=array('is_recycle'=>0);
    }
    $res = pdo_update('cqkundian_farm_integral_order', $update_order, array('uniacid' => $uniacid, 'id' => $order_id));
    if($res){
        echo json_encode(array('status'=>1,'msg'=>'操作成功'));die;
    }else{
        echo json_encode(array('status'=>2,'msg'=>'操作失败'));die;
    }
}

if($op=='send_integral_goods'){
    $order_id=$_GPC['order_id'];
    $send_number=$_GPC['send_number'];
    $orderData=pdo_get('cqkundian_farm_integral_order',array('id'=>$order_id,'uniacid'=>$uniacid));
    $save_date=array(
        'send_number'=>$send_number,
        'express_company'=>$_GPC['express_company'],
        'status'=>2,
    );
    $res=pdo_update('cqkundian_farm_integral_order',$save_date,array('id'=>$order_id,'uniacid'=>$uniacid));
    echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
}

//完成配送
if($op=='confirmIntegralGoods'){
    $order_id=$_GPC['order_id'];
    $orderData=pdo_get('cqkundian_farm_integral_order',array('id'=>$order_id,'uniacid'=>$uniacid));
    $res=pdo_update('cqkundian_farm_integral_order',array('status'=>3),array('uniacid'=>$uniacid,'id'=>$order_id));    
    echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
}


//删除订单
if($op=='deleteIntegralOrder'){
    $order_id=$_GPC['order_id'];
    $orderData=pdo_get('cqkundian_farm_integral_order',array('uniacid'=>$uniacid,'id'=>$order_id));
    if($orderData['status']==0 || $orderData['status']==5){
        $res=pdo_delete('cqkundian_farm_integral_order',array('id'=>$order_id,'uniacid'=>$uniacid));
        $res_detail=pdo_delete('cqkundian_farm_integral_order_detail',array('order_id'=>$order_id,'uniacid'=>$uniacid));
        echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
    }else{
        echo json_encode(array('status'=>3));die;
    }
}

//家畜宰杀配送订单列表
if($op=='animal_order'){
    $flag=$_GPC['flag'];
    $order_type=$_GPC['order_type'];
    $old_time=array(
        'start'=>date('Y-m-d',strtotime('-30 days')),
        'end'=>date('Y-m-d',strtotime('+1 days')),
    );
    $condition=array();
    if(!empty($_GPC['order_number'])){
        $order_number=trim($_GPC['order_number']);
        $condition['order_number LIKE']= '%'.$order_number.'%';
    }
    if($_GPC['is_recycel']){
        $is_recycel=$_GPC['is_recycel'];
        $condition['is_recycel']=$is_recycel;
    }else{
        if (!empty($_GPC['status']) && $_GPC['status'] != 6) {
            $status = $_GPC['status'];
            if ($status == 7) {
                $condition['status'] = 0;
            } else {
                $condition['status'] = $status;
            }
        } else {
            $status = 6;
        }
        $condition['is_recycel']=0;
    }
    $time=$_GPC['time'];
    if($time){
        $condition['create_time >']=strtotime($time['start']);
        $condition['create_time <']=strtotime($time['end']);
        $old_time=$time;
    }
    $condition['uniacid']=$uniacid;
    $condition['order_type']=3;
    if($flag=='true'){
        outOrder($condition,$order_type);
    }else {
        if ($_GPC['order_number'] || $_GPC['time']) {
            $list = pdo_getall("cqkundian_farm_shop_order", $condition, '', '', 'create_time desc');
            $totalMoney = 0;
            for ($i = 0; $i < count($list); $i++) {
                $totalMoney += $list[$i]['total_price'];
            }
        } else {
            $listCount = pdo_getall("cqkundian_farm_shop_order", $condition);
            $setData = pdo_get('cqkundian_farm_set', array('ikey' => 'expire_order_time', 'uniacid' => $uniacid));
            if (!empty($setData) && $setData['value'] != 0 && $setData['value'] > 0) {
                for ($i = 0; $i < count($listCount); $i++) {
                    if ($listCount[$i]['status'] == 0) {
                        if ($listCount[$i]['create_time'] + $setData['value'] <= time()) {
                            pdo_update('cqkundian_farm_shop_order', array('status' => 5), array('uniacid' => $uniacid, 'id' => $listCount[$i]['id']));
                        }
                    }
                }
            }

            $totalMoney = 0;
            for ($i = 0; $i < count($listCount); $i++) {
                $totalMoney += $listCount[$i]['total_price'];
            }
            $total = count($listCount);
            $pageSize = 15; //每页显示的数据条数
            $pageIndex = intval($_GPC['page']) ? intval($_GPC['page']) : 1;  //当前页
            $pager = pagination($total, $pageIndex, $pageSize);
            $list = pdo_getall("cqkundian_farm_shop_order", $condition, '', '', 'create_time desc', array($pageIndex, $pageSize));
        }
    }
    include $this->template("web/order/animal_order");
}

if($op=='animal_edit'){
    $order_id=$_GPC['id'];
    $orderData=pdo_get('cqkundian_farm_shop_order',array('id'=>$order_id,'uniacid'=>$uniacid));
    $order_detail=pdo_getall('cqkundian_farm_shop_order_detail',array('order_id'=>$order_id,'uniacid'=>$uniacid));
    //查询商品规格
    for($i=0;$i<count($order_detail);$i++){
        $specVal=pdo_get('cqkundian_farm_animal_sku',array('aid'=>$order_detail[$i]['goods_id'],'uniacid'=>$uniacid));
        $spec_id=explode(",", $specVal['sku_name']);
        $spec_val=pdo_getall('cqkundian_farm_animal_spec_value',array('id in'=>$spec_id));
        $specItem=pdo_getall('cqkundian_farm_animal_spec',array('aid'=>$order_detail[$i]['goods_id'],'uniacid'=>$uniacid));
        for($j=0;$j<count($specItem);$j++){
            for($m=0;$m<count($spec_val);$m++){
                if($specItem[$j]['id']==$spec_val[$m]['spec_id']){
                    $specItem[$j]['spec_value']=$spec_val[$m]['spec_value'];
                }
            }
        }
        $order_detail[$i]['specItem']=$specItem;
    }
    include $this->template("web/order/order_detail");
}

//种植摘取
if($op=='seed_order'){
    $flag=$_GPC['flag'];
    $order_type=$_GPC['order_type'];
    $old_time=array(
        'start'=>date('Y-m-d',strtotime('-30 days')),
        'end'=>date('Y-m-d',strtotime('+1 days')),
    );
    $condition=array();
    if(!empty($_GPC['order_number'])){
        $order_number=trim($_GPC['order_number']);
        $condition['order_number LIKE']= '%'.$order_number.'%';
    }

    if($_GPC['is_recycel']){
        $is_recycel=$_GPC['is_recycel'];
        $condition['is_recycel']=$is_recycel;
    }else{
        if (!empty($_GPC['status']) && $_GPC['status'] != 6) {
            $status = $_GPC['status'];
            if ($status == 7) {
                $condition['status'] = 0;
            } else {
                $condition['status'] = $status;
            }
        } else {
            $status = 6;
        }
        $condition['is_recycel'];
    }
    $time=$_GPC['time'];
    if($time){
        $condition['create_time >']=strtotime($time['start']);
        $condition['create_time <']=strtotime($time['end']);
        $old_time=$time;
    }
    $condition['uniacid']=$uniacid;
    $condition['order_type']=4;
    if($flag=='true'){
        outOrder($condition,$order_type);
    }else {
        if ($_GPC['order_number']  || $_GPC['time']) {
            $list = pdo_getall("cqkundian_farm_shop_order", $condition, '', '', 'create_time desc');
            $totalMoney = 0;
            for ($i = 0; $i < count($list); $i++) {
                $totalMoney += $list[$i]['total_price'];
            }
        } else {
            $listCount = pdo_getall("cqkundian_farm_shop_order", $condition);
            $setData = pdo_get('cqkundian_farm_set', array('ikey' => 'expire_order_time', 'uniacid' => $uniacid));
            if (!empty($setData) && $setData['value'] != 0 && $setData['value'] > 0) {
                for ($i = 0; $i < count($listCount); $i++) {
                    if ($listCount[$i]['status'] == 0) {
                        if ($listCount[$i]['create_time'] + $setData['value'] <= time()) {
                            pdo_update('cqkundian_farm_shop_order', array('status' => 5), array('uniacid' => $uniacid, 'id' => $listCount[$i]['id']));
                        }
                    }
                }
            }

            $totalMoney = 0;
            for ($i = 0; $i < count($listCount); $i++) {
                $totalMoney += $listCount[$i]['total_price'];
            }
            $total = count($listCount);
            $pageSize = 15; //每页显示的数据条数
            $pageIndex = intval($_GPC['page']) ? intval($_GPC['page']) : 1;  //当前页
            $pager = pagination($total, $pageIndex, $pageSize);
            $list = pdo_getall("cqkundian_farm_shop_order", $condition, '', '', 'create_time desc', array($pageIndex, $pageSize));
        }
    }
    include $this->template("web/order/send_order");
}

if($op=='send_edit'){
    $order_id=$_GPC['id'];
    $orderData=pdo_get('cqkundian_farm_shop_order',array('id'=>$order_id,'uniacid'=>$uniacid));
    $order_detail=pdo_getall('cqkundian_farm_shop_order_detail',array('order_id'=>$order_id,'uniacid'=>$uniacid));
    //查询商品规格
    for($i=0;$i<count($order_detail);$i++){
        $sendData=pdo_get('cqkundian_farm_send',array('id'=>$order_detail[$i]['goods_id']));
        $order_detail[$i]['goods_name']=$sendData['send_name'];
    }
    include $this->template("web/order/order_detail");
}

//订单设置
if($op=='order_set'){
    $list=pdo_get('cqkundian_farm_set',array('ikey'=>'expire_order_time','uniacid'=>$uniacid));
    include $this->template("web/order/order_set");
}

//订单设置保存
if($op=='order_set_save'){
    $data=$_POST;
    $res=0;
    foreach ($data as $key=>$v){
        $updateData=array(
            'ikey'=>$key,
            'value'=>$v,
            'uniacid'=>$uniacid,
        );
        $cond=array(
            'ikey'=>$key,
            'uniacid'=>$uniacid,
        );
        $farmData=pdo_get('cqkundian_farm_set',$cond);
        if(empty($farmData)){
            $res+=pdo_insert('cqkundian_farm_set',$updateData);
        }else{
            $res+=pdo_update('cqkundian_farm_set',$updateData,$cond);
        }
    }
    if($res){
        message('操作成功',url('site/entry/order',array('m'=>'kundian_farm','op'=>'order_set')));die;
    }else{
        message('操作失败');
    }
}

/**
 * 导出订单
 * @param $condition    //订单导出条件
 * @param $order_type   //订单类型
 */
function outOrder($condition,$order_type){
    $data[][0]=array('订单编号','订单金额','下单时间','订单状态','支付方式','收货人姓名','联系电话','快递单号','快递公司','运费','是否使用优惠券','优惠金额','商品信息');
    if($order_type==1) {
        $listCount = pdo_getall("cqkundian_farm_shop_order", $condition, '', '', 'create_time desc');
    }elseif ($order_type==2){
        $listCount = pdo_getall("cqkundian_farm_group_order", $condition, '', '', 'create_time desc');
    }elseif ($order_type==3){
        $listCount = pdo_getall("cqkundian_farm_integral_order", $condition, '', '', 'create_time desc');
    }
    //循环遍历整理卡券信息
    $orderData=array();
    for ($i=0;$i<count($listCount);$i++){
        $orderData[$i]['order_number']=' '.$listCount[$i]['order_number'];
        $orderData[$i]['total_price']=$listCount[$i]['total_price'];
        $orderData[$i]['create_time']=' '.date("Y-m-d H:i:s",$listCount[$i]['create_time']);
        if($listCount[$i]['status']==0){
            $orderData[$i]['is_pay']=" 未支付";
        }else if($listCount[$i]['status']==1){
            $orderData[$i]['is_pay']=" 待配送";
        }else if($listCount[$i]['status']==2){
            $orderData[$i]['is_pay']=" 待确认";
        }else if($listCount[$i]['status']==3){
            $orderData[$i]['is_pay']=" 已完成";
        }else if($listCount[$i]['status']==4){
            $orderData[$i]['is_pay']=" 申请取消";
        }else{
            $orderData[$i]['is_pay']=" 已取消";
        }
        $orderData[$i]['pay_method']=$listCount[$i]['pay_method'];
        $orderData[$i]['name']='  '.$listCount[$i]['name'];
        $orderData[$i]['phone']='  '.$listCount[$i]['phone'];
        $orderData[$i]['send_number']='  '.$listCount[$i]['send_number'];
        $orderData[$i]['express_company']='  '.$listCount[$i]['express_company'];
        $orderData[$i]['send_price']='  '.$listCount[$i]['send_price'];

        if($listCount[$i]['coupon_id']!=0){
            $orderData[$i]['is_use_coupon']='是';
        }else{
            $orderData[$i]['is_use_coupon']='否';
        }
        $orderData[$i]['coupon_price']=' '.$listCount[$i]['coupon_price'];
        //查询订单详细信息
        $str = '';
        if($order_type==1) {  //普通商城订单
            $order_detail = pdo_getall('cqkundian_farm_shop_order_detail', array('order_id' => $listCount[$i]['id'], 'uniacid' => $condition['uniacid']));
            for ($n = 0; $n < count($order_detail); $n++) {
                $str .= ' 商品名称：' . $order_detail[$n]['goods_name'] . ';价格：' . $order_detail[$n]['price'] . ';';
                if ($order_detail[$n]['spec_id'] != 0) {
                    $str .= '规格：';
                    $specVal = pdo_get('cqkundian_farm_goods_spec', array('goods_id' => $order_detail[$n]['goods_id'], 'uniacid' => $condition['uniacid']));
                    $spec_id = explode(",", $specVal['sku_name']);
                    $spec_val = pdo_getall('cqkundian_farm_spec_value', array('id in' => $spec_id));
                    $specItem = pdo_getall('cqkundian_farm_spec', array('goods_id' => $order_detail[$n]['goods_id'], 'uniacid' => $condition['uniacid']));
                    for ($j = 0; $j < count($specItem); $j++) {

                        for ($m = 0; $m < count($spec_val); $m++) {
                            if ($specItem[$j]['id'] == $spec_val[$m]['spec_id']) {
                                $specItem[$j]['spec_value'] = $spec_val[$m]['spec_value'];
                            }
                        }
                        $str .= $specItem[$j]['name'] . ':' . $specItem[$j]['spec_value'];
                    }
                }
            }
        }elseif ($order_type==2){       //.组团商城订单
            $order_detail=pdo_getall('cqkundian_farm_group_order_detail',array('order_id'=>$listCount[$i]['id'],'uniacid'=>$condition['uniacid']));
            //查询商品规格
            for($n=0;$n<count($order_detail);$n++){
                $str .= ' 商品名称：' . $order_detail[$n]['goods_name'] . ';价格：' . $order_detail[$n]['price'] . ';';
                if($order_detail[$n]['spec_id']!=0) {
                    $str .= '规格：';
                    $specVal = pdo_get('cqkundian_farm_group_goods_spec', array('goods_id' => $order_detail[$n]['goods_id'], 'uniacid' => $condition['uniacid']));
                    $spec_id = explode(",", $specVal['sku_name']);
                    $spec_val = pdo_getall('cqkundian_farm_group_spec_value', array('id in' => $spec_id));
                    $specItem = pdo_getall('cqkundian_farm_group_spec', array('goods_id' => $order_detail[$n]['goods_id'], 'uniacid' => $condition['uniacid']));
                    for ($j = 0; $j < count($specItem); $j++) {
                        for ($m = 0; $m < count($spec_val); $m++) {
                            if ($specItem[$j]['id'] == $spec_val[$m]['spec_id']) {
                                $specItem[$j]['spec_value'] = $spec_val[$m]['spec_value'];
                            }
                        }
                        $str .= $specItem[$j]['name'] . ':' . $specItem[$j]['spec_value'];
                    }
                }
            }
        }elseif ($order_type==3){       //.组团商城订单
            $order_detail=pdo_getall('cqkundian_farm_integral_order_detail',array('order_id'=>$listCount[$i]['id'],'uniacid'=>$condition['uniacid']));
            //查询商品规格
            for($n=0;$n<count($order_detail);$n++){
                $str .= ' 商品名称：' . $order_detail[$n]['goods_name'] . ';价格：' . $order_detail[$n]['price'] . ';';
                if($order_detail[$n]['spec_id']!=0) {
                    $str .= '规格：';
                    $specVal = pdo_get('cqkundian_farm_integral_goods_spec', array('goods_id' => $order_detail[$n]['goods_id'], 'uniacid' => $condition['uniacid']));
                    $spec_id = explode(",", $specVal['sku_name']);
                    $spec_val = pdo_getall('cqkundian_farm_integral_spec_value', array('id in' => $spec_id));
                    $specItem = pdo_getall('cqkundian_farm_integral_spec', array('goods_id' => $order_detail[$i]['goods_id'], 'uniacid' => $condition['uniacid']));
                    for ($j = 0; $j < count($specItem); $j++) {
                        for ($m = 0; $m < count($spec_val); $m++) {
                            if ($specItem[$j]['id'] == $spec_val[$m]['spec_id']) {
                                $specItem[$j]['spec_value'] = $spec_val[$m]['spec_value'];
                            }
                        }
                        $str .= $specItem[$j]['name'] . ':' . $specItem[$j]['spec_value'];
                    }
                }
            }
        }
        $orderData[$i]['goods_info']=$str;
    }
    $data[]=$orderData;
    require_once "Org/PHPExcel.class.php";
    require_once "Org/PHPExcel/Writer/Excel5.php";
    require_once "Org/PHPExcel/IOFactory.php";
    require_once "Org/function.php";
    $filename="订单";
    getExcel($filename,$data);
}

/**
 * 分销佣金发放
 * @param $orderData
 * @param $uniacid
 */
function saleSendPirce($orderData,$uniacid,$order_type){
    include 'function.inc.php';
    $res=0;
    $user=pdo_get('cqkundian_farm_user',array('uniacid'=>$uniacid,'uid'=>$orderData['uid']));
    if($user['one_distributor']!=0) {
        $one_sale = pdo_get('cqkundian_farm_user', array('uniacid' => $uniacid, 'uid' => $user['one_distributor']));
        //一级分销商加佣金
        $update_one_sale=array(
            'total_price +='=>$orderData['one_price'],
            'price +='=>$orderData['one_price'],
        );
        $res+=pdo_update('cqkundian_farm_user',$update_one_sale,array('uniacid'=>$uniacid,'uid'=>$user['one_distributor']));
        recordSalePrice($user['one_distributor'],$orderData['uid'],$orderData['one_price'],'一级分销佣金',1,$uniacid,$order_type,$orderData['id']);
        if($one_sale['one_distributor']!=0){
            $update_two_sale=array(
                'total_price +='=>$orderData['two_price'],
                'price +='=>$orderData['two_price'],
            );
            //二级分销商加佣金
            $res=pdo_update('cqkundian_farm_user',$update_two_sale,array('uniacid'=>$uniacid,'uid'=>$one_sale['one_distributor']));
            recordSalePrice($one_sale['one_distributor'],$orderData['uid'],$orderData['two_price'],'二级分销佣金',1,$uniacid,$order_type,$orderData['id']);
        }
    }else{
        $res=0;
    }
    return $res;
}
